package cn.edu.java.thread.原子类;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

@Getter
@AllArgsConstructor
class MyClass {
    public volatile String myField;
}

public class AtomicReferenceFieldUpdaterDemo {
    public static void main(String[] args) {
        AtomicReferenceFieldUpdater<MyClass, String> updater =
                AtomicReferenceFieldUpdater.newUpdater(MyClass.class, String.class, "myField");

        MyClass obj = new MyClass("Initial Value");


        System.out.println("Before update: " + obj.getMyField());

        // 原子地更新字段的值
        updater.compareAndSet(obj, "Initial Value", "Updated Value");

        System.out.println("After update: " + obj.getMyField());
    }
}

